package net.uhuijia.software.softwarerequest.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Objects;
import java.util.Random;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import cn.jiguang.common.utils.StringUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import net.uhuijia.utils.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import net.uhuijia.appbase.controller.BaseController;
import net.uhuijia.enums.MoreInfo;
import net.uhuijia.software.softwarerequest.persistence.model.SoftwareRequest;
import net.uhuijia.software.softwarerequest.persistence.model.SoftwareRequestExample;
import net.uhuijia.software.softwarerequest.service.SoftwareRequestService;
import net.uhuijia.user.usermall.clientservice.UserMallClientService;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.ResultMapVo;
import net.uhuijia.vo.ReturnListVo;
import net.uhuijia.software.softwareQcode.service.SoftwareQcodeService;

import static java.awt.image.ImageObserver.HEIGHT;
import static java.awt.image.ImageObserver.WIDTH;


@RestController
@RequestMapping(value="/softwarerequest")
public class SoftwareRequestController
     extends BaseController<SoftwareRequestService,SoftwareRequest,SoftwareRequestExample> {
     //添加用户请求post
	@Autowired
	private UserMallClientService userMallClientService;
	@Autowired
    private SoftwareQcodeService softwareQcodeService;

	/**
	 * 添加用户请求
	 * @param request 请求信息
	 * @param token 用户token
	 * @param device 登入设备
	 * @return 返回
	 */
	@RequestMapping(value = "/addRequest")
	@ResponseBody
	public ResultMapVo<?> addRequest( SoftwareRequest request, String token, String device ){
		if( StringHelper.isNotBlanks(request.getRequestAddress(),request.getMethod())){
			if( StringHelper.isNotBlanks(token,device) ){
				UserMall user = userMallClientService.auth(device, token);
				if( null != user ){
					request.setUserId(user.getId());
					request.setNickName(user.getNickName());
				}
			}
			s.add(request);
			return ResultMapVo.ok();
		}else{
			throw new IllegalArgumentException();
		}
	}

	/**
	 * 用户的历史请求信息
	 * @param request 请求数据
	 * @return 返回信息
	 */
	@RequestMapping(value = "/history")
	@ResponseBody
	public ResultMapVo<ReturnListVo<Map<String, Object>>> history( HttpServletRequest request ){
		UserMall user = this.getUserMallByTokenAndDevice();
		List<SoftwareRequest> list = s.listUserHistoryRequestAndPage(request,user);
		List<Map<String, Object>> tranList2MapList = ValueHelper.tranList2MapList(list, ignoreProperties);
		Integer count = s.countUserHistoryRequest(user);
		MoreInfo moreInfo = IticConst.isMore(request,count);
		ReturnListVo<Map<String, Object>> resultList = new ReturnListVo<>(tranList2MapList,count, moreInfo);
		return ResultMapVo.ok(resultList);
	}

	 //用户历史请求
	 //用户生成维码
	 //用户历史二维码
	 //icon 列表展示
	/**
	 * 二维码生成
	 * @param outputStream 文件输出流路径
	 * @param content      二维码携带信息
	 * @param qrCodeSize   二维码图片大小
	 * @param imageFormat  二维码的格式
	 * @throws WriterException
	 * @throws IOException
	 */
	@RequestMapping(value = "/createQRcode")
	@ResponseBody
	public ResultMapVo<JSONObject> createQRcode(String token,String device,SoftwareRequest request,
		OutputStream outputStream, String content, Integer qrCodeSize, String imageFormat)
			throws IOException, WriterException {
        String body = null;
        JSONObject results = null;
        String result = "";
        //生成器中会减掉200 这里预处理
		imageFormat = "png";
		Random random = new Random();
		File QrcodeFile = new File("D:\\\\" + random.nextInt() + "." + imageFormat);
		outputStream = new FileOutputStream(QrcodeFile);
		QrCodeCreateUtil.createQrCode(outputStream, content, qrCodeSize, imageFormat);
		results = InputStreamUpload.upload(QrcodeFile.getPath(),random.nextInt());
		//删除本地文件
		InputStreamUpload.deleteLocalFile(QrcodeFile.getPath());
        if(StringHelper.isNotBlanks(token,device)){
            UserMall user = userMallClientService.auth(device, token);
			//添加二维码图片到数据表
			if(results != null){
				String url = (String) JSONObject.fromObject(results.get("data")).get("url");
				result = softwareQcodeService.addData(url,user, qrCodeSize);
			}
            return ResultMapVo.ok(results);
		}else{
            return ResultMapVo.ok(results);
        }
	}


}